package com.meng.system.service.impl.auth;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.meng.system.domain.vo.SysMenuVO;
import com.meng.system.domain.vo.SysUserVO;
import com.meng.system.domain.vo.UserInfoVO;
import com.meng.system.domain.vo.UserSaveVO;
import com.meng.system.mapper.auth.SysMenuMapper;
import com.meng.system.mapper.auth.SysRoleMapper;
import com.meng.system.mapper.auth.SysUserMapper;
import com.meng.system.security.MiniUserDetails;
import com.meng.system.service.auth.SysUserService;
import com.meng.system.utils.CopyUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author Xu Meng
 * @since 2021-01-14
 */
@Slf4j
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUserVO> implements SysUserService {

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private SysRoleMapper sysRoleMapper;

    @Autowired
    private SysMenuMapper sysMenuMapper;

    @Override
    public SysUserVO getUserVoByLoginName(String loginName) {
        return sysUserMapper.findUsernameByUser(loginName);
    }

    /**
     * 根据用户id 获取 roleids
     */
    @Override
    public List<String> findUserIdByRolesId(Long userId) {
        return sysRoleMapper.findUserIdByRolesId(userId);
    }

    /**
     * 新增or修改用户
     * @param user 新增or修改参数
     * @return  flag  成功or失败
     */
    @Override
    public boolean saveOrUpdateUser(UserSaveVO user) throws Exception {
        SysUserVO userVo = new SysUserVO();
        if (Objects.isNull(user.getUserId())) {
            //新增
            userVo.setCreateTime(new Date());
            userVo.setUpdateTime(new Date());
            userVo.setDelFlag("0");

            CopyUtils.copy(user,userVo);

            int insert = sysUserMapper.insert(userVo);
            if (insert > 0) {
                List<String> roleCodes = user.getRoleIds();
                List<Long> roleIds = sysRoleMapper.findRoleIds(roleCodes);
                int flag = sysRoleMapper.addRolesByUserId(user.getUserId(),roleIds);
                return flag > 0;
            }
            return false;
        } else {
            //修改
            CopyUtils.copy(user,userVo);
            userVo.setUpdateTime(new Date());
            return sysUserMapper.updateById(userVo) > 0;
        }
    }

    /**
     * 删除用户
     * @param userId 用户id
     * @return result
     */
    @Override
    public boolean delUser(Long userId) {
        SysUserVO user = new SysUserVO();
        user.setUserId(userId);
        user.setDelFlag("1");
        return sysUserMapper.updateById(user) > 0;
    }

    /**
     * 获取用户列表
     * @return result
     */
    @Override
    public Page<SysUserVO> findUserList(int pageNum, int pageSize, String search) {
        Page<SysUserVO> page = new Page<>(pageNum, pageSize);
        List<SysUserVO> result = sysUserMapper.findUserList(search,page);
        page.setRecords(result);
        return page;
    }

    /**
     * 获取当前用户登录信息
     * @param user 用户信息
     */
    @Override
    public UserInfoVO getUserInfoByUserId(MiniUserDetails user) {
        //获取当前用户菜单列表
        UserInfoVO userInfoVO = new UserInfoVO();
        SysUserVO userVO = sysUserMapper.findUsernameByUser(user.getUsername());
        List<SysMenuVO> menuTreeList = sysMenuMapper.findMenuTreeList(userVO.getUserId());
        try {
            CopyUtils.copy(userVO,userInfoVO);
            userInfoVO.setMenuList(menuTreeList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return userInfoVO;
    }
}
